package cn.wolfcode.p2p.business.controller;

import cn.wolfcode.p2p.base.query.BidRequestQueryObject;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.util.Constant;
import cn.wolfcode.p2p.util.JSONResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 借款审核
 */
@Controller
public class BidRequestAuditController {

    @Autowired
    private IBidRequestService bidRequestService;

    /**
     * 借款审核页面
     * @param qo
     * @return
     */
    @RequestMapping("/bidrequest_publishaudit_list")
    public String publishaudit_list(@ModelAttribute("qo")BidRequestQueryObject qo,Model model){
        qo.setBidRequestState(Constant.BIDREQUEST_STATE_PUBLISH_STATE_APPLY);
        int[] bidRequestTypes = {Constant.BIDREQUEST_TYPE_NORMAL,Constant.EXPERIENCE_BIDREQUEST_TYPE_CREDITTRANSFER};
        qo.setBidRequestTypes(bidRequestTypes);
        model.addAttribute("pageResult",bidRequestService.queryForPage(qo));
        return "bidrequest/publish_audit";

    }

    /**
     * 借款待审核
     * @param id 借款id
     * @param remark 备注
     * @param state 审核结果
     * @param publishTime 发布时间
     * @return
     */
    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public Object publishBeforeAudit(Long id, String remark, Integer state, @DateTimeFormat(pattern = "yyyy-MM-dd")Date publishTime){
        bidRequestService.publishBeforeAudit(id,remark,state,publishTime);
        return JSONResultUtil.jsonResult();
    }

    /**
     * 满标一审页面
     */

    @RequestMapping("bidrequest_audit1_list")
    public String bidRequestAudit1List(@ModelAttribute("qo")BidRequestQueryObject qo,Model model){
        qo.setBidRequestState(Constant.BIDREQUEST_STATE_APPROVE_PENDING_1);
        model.addAttribute("pageResult",bidRequestService.queryForPage(qo));
        return "bidrequest/audit1";
    }

    /**
     *  满标一审
     * @param id
     * @param remark
     * @param state
     */
    @RequestMapping("bidrequest_audit1")
    @ResponseBody
    public Object bidRequestAudit1(Long id,String remark,Integer state){
        bidRequestService.bidRequestAudit1(id,remark,state);
        return JSONResultUtil.jsonResult();
    }

    /**
     * 满标二审页面
     */
    @RequestMapping("bidrequest_audit2_list")
    public String bidRequestAudit2List(@ModelAttribute("qo")BidRequestQueryObject qo,Model model){
        qo.setBidRequestState(Constant.BIDREQUEST_STATE_APPROVE_PENDING_2);
        model.addAttribute("pageResult",bidRequestService.queryForPage(qo));
        return "bidrequest/audit2";
    }

    /**
     *  满标二审
     * @param id
     * @param remark
     * @param state
     */
    @RequestMapping("bidrequest_audit2")
    @ResponseBody
    public Object bidRequestAudit2(Long id,String remark,Integer state,Integer bidRequestType){
        if(bidRequestType == Constant.BIDREQUEST_TYPE_NORMAL){
            bidRequestService.bidRequestAudit2(id,remark,state);
        }else {
            bidRequestService.expBidRequestAudit2(id,remark,state);
        }
        return JSONResultUtil.jsonResult();
    }
}
